var NEIGHBORHOODS = new Array();
NEIGHBORHOODS.push( new Neighborhood("paris1","kml/paris1.kml"));
NEIGHBORHOODS.push( new Neighborhood("paris2","kml/paris2.kml"));
NEIGHBORHOODS.push( new Neighborhood("paris3","kml/paris3.kml"));

viewer= null;
map=null;
shapes = [];
markers = [];
overlays = [];

function initGeoportalMap(latitude, longitude) {
    if (typeof(geoportalLoadmap)!='function') {
        console.log("initGeoportalMap() geoportalLoadviewer function");
        return;
    }
    geoportalLoadmap("map_canvas", "normal", 'FXX');
    if (!map) {
        console.log("initGeoportalMap() geoportalLoadviewer");
        return;
    }
    map.getMap().addControl(new OpenLayers.Control.LoadingPanel());
    kbControl= map.getMap().getControlsByClass(OpenLayers.Control.KeyboardDefaults.prototype.CLASS_NAME)[0];
    map.addGeoportalLayers([
        'ORTHOIMAGERY.ORTHOPHOTOS:WMSC',
        'GEOGRAPHICALGRIDSYSTEMS.MAPS:WMSC'
    ]);
    map.getMap().setCenterAtLonLat(longitude, latitude, 11);
    var tbx= map.getMap().getControlsByClass('Geoportal.Control.ToolBox')[0];
}

function showMap(latitude, longitude) {
	MAP.setCenter(new CM.LatLng(latitude, longitude), 11);

	clearOverlays();
}

function clearOverlays() {
	for (var i = 0; i < NEIGHBORHOODS.length; i++) {
		NEIGHBORHOODS[i].drawit = false;
    }
	
	if (overlays) {
		for (i in overlays) {
			MAP.removeOverlay(overlays[i]);
		}
	}
	
	markers = [];
	shapes = [];
}

function resetMap(longitude, latitude) {
	map.getMap().setCenterAtLonLat(longitude, latitude, 11);
}

function showArrondissement(latitude, longitude, arrondissement) {
	map.getMap().setCenterAtLonLat(latitude,longitude, 11);
	for (var i = 0; i < NEIGHBORHOODS.length; i++) {
		if (arrondissement == NEIGHBORHOODS[i].id) {
			NEIGHBORHOODS[i].drawit = true;
			break;
		}
    }

    addOverlays();
}

function addOverlays() {
	// ADD OVERLAY
	for (var i = 0; i < NEIGHBORHOODS.length; i++) {
		if (NEIGHBORHOODS[i].drawit) {
			if (contains(shapes,NEIGHBORHOODS[i].id)) {
				continue;
			}
			
			//ajout fichier KML
			map.getMap().addLayer("KML", NEIGHBORHOODS[i].id, NEIGHBORHOODS[i].kml);

			//position
			map.openLayersPanel(true);
			map.openToolsPanel(true);
			
			shapes.push(NEIGHBORHOODS[i].id);
		}
	}
}


function addStaticMarker(id, lat, lng) {
	if (!contains(markers,id)) {
		var position = new OpenLayers.Geometry.Point(lng, lat);
		position.transform(OpenLayers.Projection.CRS84, map.projection);		
		var style = {externalGraphic:'http://www.ailleursloin.free.fr/kml/marker-gold.png', graphicWidth:21, graphicHeight:25 };
		var pt = new OpenLayers.Feature.Vector(position, null, style);

		//couche du marqueur
		var couche = new OpenLayers.Layer.Vector('Static marker');
		couche.addFeatures([pt]);
		map.getMap().addLayer(couche);
	}
	
	markers.push(id);
}

function addDraggableMarker(id, lat, lng) {
	if (!contains(markers,id)) {
		var position = new OpenLayers.Geometry.Point(lng, lat);
		position.transform(OpenLayers.Projection.CRS84, map.projection);		
		var style = {externalGraphic:'http://www.ailleursloin.free.fr/kml/marker-gold.png', graphicWidth:21, graphicHeight:25 };
		var pt = new OpenLayers.Feature.Vector(position, null, style);

		//couche du marqueur
		var couche = new OpenLayers.Layer.Vector('Draggable marker');
		couche.addFeatures([pt]);
		map.getMap().addLayer(couche);
		
		drag = new OpenLayers.Control.DragFeature(couche, {onComplete: function(f)	{}});
		 
		map.getMap().addControl(drag);
		drag.activate();
	}
	
	markers.push(id);
}


function contains(a, obj) {
    var i = a.length;
    while (i--) {
       if (a[i] === obj) {
           return true;
       }
    }
    return false;
}

function Neighborhood(id, kml) {
    this.id = id;
    this.drawit = false;
    this.kml = kml;
}